<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [管理员模型]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\facade\Db;
use think\facade\Event;

class Admin extends Base
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'add_time';

    protected $pk = 'admin_id';
    protected $validate = 'Admin';

    public static function login($code)
    {
        $data = input('post.');
        if ($code == 1) {
            if (!captcha_check($data['vercode'])) {
                return json(['code' => 1, 'msg' => '验证码错误']);
            }
        }
        $user = self::getByUsername($data['username']);
        if ($user) {
            if ($user['is_open'] == 1 && $user['pwd'] == md5($data['password'] . $user['mdemail'])) {
                $adminId = $user['admin_id'];
                //更新登录IP和登录时间
                self::where('admin_id', $adminId)->update(['logintime' => time(), 'ip' => request()->ip()]);
                //查找规则
                $rules = Db::name('auth_group_access')
                    ->alias('a')
                    ->leftJoin('auth_group ag', 'a.group_id = ag.id')
                    ->field('a.group_id,ag.rules,ag.title')
                    ->where('uid', $adminId)
                    ->find();
                //查询所有不验证的方法并放入规则中
                $authOpen = AuthRule::where('authopen', 0)->select()->toArray();
                $authRole = AuthRule::select();
                $authOpens = [];
                foreach ($authOpen as $k => $v) {
                    $authOpens[] = $v['id'];
                    //查询所有下级权限
                    $ids = getChildsRule($authRole, $v['id']);
                    foreach ($ids as $kk => $vv) {
                        $authOpens[] = $vv['id'];
                    }
                }
                $authOpensStr = !empty($authOpens) ? implode(",", $authOpens) : '';
                $rules['rules'] = $rules['rules'] . ',' . $authOpensStr;

                session('admin', [
                    'id' => $user['admin_id'],
                    'username' => $user['username'],
                    'logintime' => date('Y-m-d H:i:s', $user['logintime']),
                    'loginip' => $user['ip'],
                ]);
                session('admin.group_id', $rules['group_id']);
                session('admin.rules', explode(',', $rules['rules']));
                session('admin.title', $rules['title']);
                //触发登录成功事件
                Event::trigger('AdminLogin', $user);
                return json(['code' => 0, 'msg' => '登录成功!']); //信息正确
            } else {
                return json(['code' => 1, 'msg' => '用户名或者密码错误，重新输入!']); //密码错误
            }
        } else {
            return json(['code' => 1, 'msg' => '用户不存在!']); //用户不存在
        }
    }

    public static function getInfo($admin_id)
    {
        $info = self::where('admin_id', $admin_id)->withoutField('pwd')->find();
        return $info;
    }

    public static function get_list()
    {
        //查出所有数据
        $list = Db::name('admin')
            ->alias('a')
            ->leftJoin('auth_group_access ac', 'a.admin_id = ac.uid')
            ->leftJoin('auth_group ag', 'ac.group_id = ag.id')
            ->field('a.*, ac.group_id, ag.title')
            ->order('a.admin_id asc')
            ->select();
        return $list;
    }

    public function check($code)
    {
        return captcha_check($code);
    }
}
